﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

public partial class admin_Quanlydaily : System.Web.UI.Page
{
    DaiLyController DaiLyCtr = new DaiLyController();
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable Employee = (DataTable)Session["Employee"];
        if (Employee == null)
            Response.Redirect("login.aspx");
        if (!IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
            LoadData();
        }
    }

    private void LoadData()
    {
        try
        {
            DaiLyCtr = new DaiLyController();
            gvDaiLy.DataSource = DaiLyCtr.fnC_GetDataDaily();
            gvDaiLy.DataBind();
        }
        catch { }
    }
    protected void gvDaiLy_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDaiLy.PageIndex = e.NewPageIndex;
        gvDaiLy.DataSource = DaiLyCtr.fnC_GetDataDaily();
        DataBind();
    }
    protected void gvDaiLy_SelectedIndexChanged(object sender, EventArgs e)
    {
        HiddenField madl = (HiddenField)gvDaiLy.SelectedRow.Cells[0].FindControl("hdfmasp");
        try
        {
            MultiView1.ActiveViewIndex = 1;
            DaiLyEntity daily = DaiLyCtr.fnC_GetDaiLy(madl.Value);
            hdlid.Value = daily.Iddl.ToString();
            txtDiaChi.Text = daily.DiaChi;
            txtDienThoai.Text = daily.DienThoai;
            imgLogo.ImageUrl = "../images/Daily/"+daily.Logo;
            txtEmail.Text = daily.Email;
            txtFax.Text = daily.Fax;
            txtMoTa.Text = daily.Mota;
            txtTenDaiLy.Text = daily.TenDaiLy;
            btnAddnew.Visible = false;
            btnEdit.Visible = true;
            lbhead.Text = "Chỉnh sửa thông tin đại lý";
        }
        catch { }
    }
    protected void gvDaiLy_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            HiddenField masp = (HiddenField)gvDaiLy.Rows[e.RowIndex].Cells[0].FindControl("hdfmasp");
            DaiLyEntity daily = DaiLyCtr.fnC_GetDaiLy(masp.Value);
            DaiLyCtr.DeleteDaiLy(masp.Value);
            File.Delete(Server.MapPath("~/images/DaiLy/") + daily.Logo + "");
            LoadData();
        }
        catch { }
    }
    protected void gvDaiLy_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn = (ImageButton)e.Row.Cells[6].Controls[0];
            btn.OnClientClick = "if(!confirm('Bạn có muốn xóa đại lý " + Server.HtmlDecode(e.Row.Cells[1].Text) + "  không?'))return false";
        }
    }
    protected void btnAddnew_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(fLogo.FileName))
        {
            fLogo.PostedFile.SaveAs(Server.MapPath("~/images/DaiLy/") + fLogo.FileName + "");
        }
        DaiLyEntity daily = new DaiLyEntity();
        daily.DiaChi = txtDiaChi.Text;
        daily.DienThoai = txtDienThoai.Text;
        daily.Email = txtEmail.Text;
        daily.Fax = txtFax.Text;
        daily.Iddl = 0;
        daily.Logo = fLogo.FileName;
        daily.Mota = txtMoTa.Text;
        daily.TenDaiLy = txtTenDaiLy.Text;
        DaiLyCtr.AddNew(daily);
        LoadData();
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        DaiLyEntity daily = new DaiLyEntity();
        if (!String.IsNullOrEmpty(fLogo.FileName))
        {
            try
            {
                File.Delete(imgLogo.ImageUrl.Replace('\\','/'));
            }
            catch { }
            fLogo.PostedFile.SaveAs(Server.MapPath("~/images/DaiLy/") + fLogo.FileName + "");
            daily.Logo = fLogo.FileName;
        }
        else
            daily.Logo = imgLogo.ImageUrl;
        daily.Iddl = Int32.Parse( hdlid.Value);
        daily.DiaChi = txtDiaChi.Text;
        daily.DienThoai = txtDienThoai.Text;
        daily.Email = txtEmail.Text;
        daily.Fax = txtFax.Text;
        daily.Mota = txtMoTa.Text;
        daily.TenDaiLy = txtTenDaiLy.Text;
        DaiLyCtr.UpdateDaiLy(daily);
        LoadData();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btnAddState_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        btnAddnew.Visible = true;
        btnEdit.Visible = false;
        lbhead.Text = "Thêm Mới Đại Lý";
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        LoadData();
        MultiView1.ActiveViewIndex = 0;
    }
}